package es.javamyadmin.dao;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collection;

import es.javamyadmin.modelo.Triggers;
import es.javamyadmin.pojo.Error;
import es.javamyadmin.pojo.Mensaje;
/**
 * Clase con los distintos metodos para trabajar con los TRIGGERS de MySQL. 
 * @author emilio
 *
 */
public class TriggersDAO {

	Connection conexion = null;
	Statement estamento = null;
	PreparedStatement preparada = null;
	ResultSet resultado = null;
	InputStream path = null;
	
	/**
	 * Metodo que lee los triggers
	 * @param user
	 * @param pass
	 * @param host
	 * @param port
	 * @param database
	 * @return Collection
	 */
	public Collection<Triggers> getTriggers(String user, String pass, String host, String port, String database){
		Collection<Triggers> listaTriggers = new ArrayList<Triggers>();
		try {
			Class.forName("com.mysql.jdbc.Driver");
      	    conexion=DriverManager.getConnection("jdbc:mysql://"+host+":"+port+"/"+database, user, pass);
      	    
      	    PreparedStatement pstm = conexion.prepareStatement("SHOW TRIGGERS");
      	    resultado = pstm.executeQuery();
			while(resultado.next()){
				Triggers trigger = new Triggers();
				trigger.setNombre(resultado.getString(1));
				trigger.setEvento(resultado.getString(2));
				trigger.setTabla(resultado.getString(3));
				trigger.setEstamento(resultado.getString(4));
				trigger.setTiming(resultado.getString(5));
				trigger.setCreated(resultado.getString(6));
				trigger.setSqlMode(resultado.getString(7));
				trigger.setDefinidcion(resultado.getString(8));
				trigger.setCharacter(resultado.getString(9));
				trigger.setCollation(resultado.getString(10));
				trigger.setDatabaseCollation(resultado.getString(11));
				listaTriggers.add(trigger);				
			}
		} catch(SQLException | ClassNotFoundException ex){
			ex.printStackTrace();
		} finally {
			cerrarConexion();
		}
		return listaTriggers;
	}
	
	/**
	 * Metodo que borra un trigger
	 * @param database
	 * @param user
	 * @param pass
	 * @param host
	 * @param port
	 * @param name
	 * @return boolean
	 */
	public boolean deleteTrigger(String database, String user, String pass, String host, String port, String name){
		boolean bien = true; 
		try {
      	    Class.forName("com.mysql.jdbc.Driver");
      	  System.out.println("jdbc:mysql://"+host+":"+port+"/"+database + " user " +  user + ", pass" + pass);
    	  conexion=DriverManager.getConnection("jdbc:mysql://"+host+":"+port+"/"+database, user, pass);
      	  PreparedStatement pstm = conexion.prepareStatement("DROP TRIGGER IF EXISTS " + name );
      	  pstm.executeUpdate();
		} catch(SQLException ex){
			bien = false;
			ex.printStackTrace();
		} catch (ClassNotFoundException e) {
			bien = false;
			e.printStackTrace();
		} finally {
			cerrarConexion();
		}
		return bien;
	}
	
	/**
	 * Metodo que crea un trigger
	 * @param database
	 * @param user
	 * @param pass
	 * @param host
	 * @param port
	 * @param trigger
	 * @return Error
	 */
	public Error createTrigger(String database, String user, String pass, String host, String port, String trigger){
		Error error = null;
		try {
      	    Class.forName("com.mysql.jdbc.Driver");
      	  System.out.println("jdbc:mysql://"+host+":"+port+"/"+database + " user " +  user + ", pass" + pass);
    	  conexion=DriverManager.getConnection("jdbc:mysql://"+host+":"+port+"/"+database, user, pass);
      	  PreparedStatement pstm = conexion.prepareStatement(trigger);
      	  pstm.executeUpdate();
		} catch(SQLException ex){
			error = new Error(Mensaje.getMensaje(path, String.valueOf(ex.getErrorCode())), ex.getMessage().replace("'", ""), "");
			ex.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} finally {
			cerrarConexion();
		}
		return error;
	}
	

	/**
	 * Metodo que cierra la conexion a mysql
	 */
	public void cerrarConexion(){
		if(resultado != null){
			try {
				resultado.close();
			} catch(SQLException e){
				e.printStackTrace();
			}
			resultado = null;
		}
		if(estamento != null){
			try {
				estamento.close();
			} catch(SQLException e){
				e.printStackTrace();
			}
			estamento = null;
		}
		if(preparada != null){
			try {
				preparada.close();
			} catch(SQLException e){
				e.printStackTrace();
			}
			preparada = null;
		}
		if(conexion != null){
			try {
				conexion.close();
			} catch(SQLException e){
				e.printStackTrace();
			}
			conexion = null;
		}
	}


	public InputStream getPath() {
		return path;
	}


	public void setPath(InputStream path) {
		this.path = path;
	}
	
}
